package com.myk.game.gobangchess.dates;

import com.google.gson.Gson;
import com.myk.game.gobangchess.Account;
import com.myk.game.gobangchess.beans.PlayerAmountFlowBean;
import com.myk.game.gobangchess.beans.PlayerAssetsBean;
import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.DateTimeUtil;
import com.myk.game.gobangchess.utils.FileUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * 逆界代币数据相关处理类
 */
public class AmountData {

    /**
     * 获取资产明细信息
     */
    public static String getAmountDetail(Account account) {
        String amount = "0";
        String amountFlowMap = "-1";

        Gson gson = new Gson();
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        if (FileUtils.isFileExists(assetsFile)) {
            String assetsBeanJson = FileUtils.readFile2String(assetsFile);
            PlayerAssetsBean assetsBean = gson.fromJson(assetsBeanJson, PlayerAssetsBean.class);
            if (assetsBean != null) {
                amount = String.valueOf(assetsBean.getAmount());
            }
        }
        String flowFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.AMOUNT_FLOW_FILE;
        if (FileUtils.isFileExists(flowFile)) {
            String flowJson = FileUtils.readFile2String(flowFile);
            PlayerAmountFlowBean flowBean = gson.fromJson(flowJson, PlayerAmountFlowBean.class);
            if (flowBean != null) {
                StringBuilder sb = new StringBuilder();
                for (PlayerAmountFlowBean.AmountFlowBean amountFlowBean : flowBean.getAmountFlowList()) {
                    sb.append(amountFlowBean.getAmount()).append(",").append(amountFlowBean.getTime()).append(",").append(amountFlowBean.getReason()).append(";");
                }
                String s = sb.toString();
                if (s.isEmpty()) {
                    amountFlowMap = "-1";
                }
                else {
                    amountFlowMap = s.substring(0, s.length() - 1);
                }
            }
        }

        return account.getIdCode() + "&" + account.getNickName() + "&" + amount + "&" + amountFlowMap;
    }

    /**
     * 指定用户修改代币
     */
    public static boolean changePlayerAmount(Account account, int amount, String reason) {
        Gson gson = new Gson();
        String assetsFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_ASSETS_FILE;
        FileUtils.createOrExistsFile(assetsFile);
        String assetsBeanJson = FileUtils.readFile2String(assetsFile);
        PlayerAssetsBean assetsBean = gson.fromJson(assetsBeanJson, PlayerAssetsBean.class);
        if (assetsBean == null) assetsBean = new PlayerAssetsBean();
        int newAmount = assetsBean.getAmount() + amount;
        assetsBean.setAmount(newAmount);
        String assetsBeanJsonNew = gson.toJson(assetsBean);
        boolean saveFlag = FileUtils.writeFileFromString(assetsFile, assetsBeanJsonNew, false);

        if (saveFlag) {
            String flowFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.AMOUNT_FLOW_FILE;
            FileUtils.createOrExistsFile(flowFile);
            String flowBeanJson = FileUtils.readFile2String(flowFile);
            PlayerAmountFlowBean flowBean = gson.fromJson(flowBeanJson, PlayerAmountFlowBean.class);
            if (flowBean == null) flowBean = new PlayerAmountFlowBean();
            if (flowBean.getAmountFlowList() == null) flowBean.setAmountFlowList(new ArrayList<>());
            String time = DateTimeUtil.localDateTimeToStr(LocalDateTime.now(), DateTimeUtil.ALL_FORMAT);
            flowBean.getAmountFlowList().add(new PlayerAmountFlowBean.AmountFlowBean(amount, time, reason));
            String flowBeanJsonNew = gson.toJson(flowBean);
            return FileUtils.writeFileFromString(flowFile, flowBeanJsonNew, false);
        }
        return false;
    }
}
